<template>
  <div>
    <div class="addcate">
        <span>轮播图列表</span>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addimageFN">上传轮播图</el-button>
    </div>
    <!-- 编辑的对话框 -->
    <el-dialog title="上传轮播图" :visible.sync="dialogFormVisible" width="600px" @close="dialogCloseFN">
          <el-form ref="addref" :model="imagesForm" :rules="addimgRules" label-position="left">
            <el-form-item label="上传图片信息" :label-width="formLabelWidth" prop="img_url">
              <el-checkbox-group v-model="imagesForm.img_url" v-show="false"></el-checkbox-group>
              <img src="../../../assets/images/addPicture.jpg" alt="" ref="imgRef" class="addimgsize">
              <input type="file" style="display:none" ref="iptRef" accept="image/*" @change="oncoverChangeFN">
              <br>
              <el-button size="mini" type="text" @click="clickipt">上传图片</el-button>
            </el-form-item>
            <el-form-item label="添加轮播图描述信息" :label-width="formLabelWidth" prop="img_describe">
              <el-input type="textarea" v-model="imagesForm.img_describe" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button @click="confimFn" type="primary">确 定</el-button>
          </div>
      </el-dialog>
      <el-table
        :data="images"
        style="width:100%">
        <el-table-column
        fixed
        type="index"
        label="序号"
        width="50">
        </el-table-column>
        <el-table-column
        fixed
        prop="img_url"
        label="轮播图封面"
        width="200">
        <template slot-scope="scope">
          <img src="../../../assets/images/addPicture.jpg" alt="" v-if="!scope.row.img_url" class="imgsize">
          <img :src="scope.row.img_url" alt="" v-else class="imgsize">
        </template>
        </el-table-column>
        <el-table-column
        prop="img_describe"
        label="描述"
        width="200">
        </el-table-column>
        <el-table-column
        prop="show"
        label="是否在首页显示"
        width="80">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-circle-check" circle v-if="scope.row.img_show == 1" @click="setShow(scope.row)"></el-button>
          <el-button type="text" icon="el-icon-circle-close" circle v-else @click="setShow(scope.row)"></el-button>
        </template>
        </el-table-column>
        <el-table-column
            label="操作"
            width="200">
            <template slot-scope="scope">
                <el-button
                size="mini"
                @click="handleEdit(scope.row)"><i class="el-icon-edit"></i></el-button>
                <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"><i class="el-icon-delete"></i></el-button>
            </template>
        </el-table-column>
     </el-table>
  </div>
</template>

<script>
import { uploadimageAPI, getimageAPI, updateimageAPI, deleteimageAPI, showimageAPI } from '@/api'
import imgUrl from '@/assets/images/addPicture.jpg'
export default {
  name: 'admin-images',
  data () {
    return {
      images: [],
      imagesForm: {
        img_url: '',
        img_describe: ''
      },
      addimgRules: {
        img_url: [
          { required: true, message: '请上传轮播图', trigger: 'change' }
        ],
        img_describe: [
          { required: true, message: '请输入轮播图描述', trigger: 'blur' }
        ]
      },
      formLabelWidth: '150px',
      dialogFormVisible: false,
      // isEdit 修改true/添加false
      isEdit: false,
      imgID: ''
    }
  },
  created () {
    this.getImagesList()
  },
  methods: {
    // 重置表单
    dialogCloseFN () {
      this.$refs.addref.resetFields()
      this.imagesForm.img_url = null
      this.$refs.imgRef.setAttribute('src', imgUrl)
    },
    // 获取轮播图列表
    async getImagesList () {
      const { data } = await getimageAPI()
      this.images = data.data
    },
    // 添加轮播图
    addimageFN () {
      this.isEdit = false
      this.dialogFormVisible = true
    },
    clickipt () {
      this.$refs.iptRef.click()
    },
    oncoverChangeFN (e) {
      const file = e.target.files
      if (file.length === 0) {
        this.imagesForm.img_url = null
        this.$refs.imgRef.setAttribute('src', imgUrl)
      } else {
        const fr = new FileReader()
        fr.readAsDataURL(file[0])
        fr.onload = (e) => {
          this.$refs.imgRef.setAttribute('src', e.target.result)
          this.imagesForm.img_url = e.target.result
        }
      }
    },
    // 编辑按钮 - 将isEdit修改为true （修改） 传入id打开对话框
    handleEdit (row) {
      this.isEdit = true
      this.imgID = row.id
      this.dialogFormVisible = true
      // 用户点击修改 - 则将下面的属性赋值为初始值
      this.$nextTick(() => {
        this.imagesForm.img_url = row.img_url
        this.imagesForm.img_describe = row.img_describe
        this.$refs.imgRef.setAttribute('src', this.imagesForm.img_url)
      })
    },
    // 确定添加轮播图
    confimFn () {
      this.$refs.addref.validate(async valid => {
        if (valid) {
          if (this.isEdit) {
            // 修改
            this.imagesForm.id = this.imgID
            const { data } = await updateimageAPI(this.imagesForm)
            if (data.status !== 0) return this.$message.error(data.message)
            this.$message.success(data.message)
          } else {
            // 添加
            const { data } = await uploadimageAPI(this.imagesForm)
            if (data.status !== 0) return this.$message.error(data.message)
            this.$message.success(data.message)
          }
          this.getImagesList()
          this.dialogFormVisible = false
        } else {
          return false
        }
      })
    },
    // 删除轮播图
    async handleDelete (row) {
      const id = row.id
      const { data } = await deleteimageAPI(id)
      if (data.status !== 0) return this.$message.error(data.message)
      this.$message.success(data.message)
      this.getImagesList()
    },
    // 设置是否在首页显示
    async setShow (item) {
      if (item.img_show === 0) {
        const { data } = await showimageAPI(1, item.id)
        if (data.status !== 0) return this.$message.error(data.messsage)
        this.$message.success(data.message)
      } else {
        const { data } = await showimageAPI(0, item.id)
        if (data.status !== 0) return this.$message.error(data.messsage)
        this.$message.success(data.message)
      }
      this.getImagesList()
    }
  }
}
</script>

<style scoped>
   .addcate{
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
   }
   .el-input{
    width:300px;
   }
   .el-table {
    border-radius: 8px;
  }
  .el-button {
    border-radius: 8px;
  }
  .imgsize {
    width: 120px;
    height: 80px;
  }
  .addimgsize {
    width: 150px;
    height: 100px;
  }
  ::v-deep .el-input__inner{
    height: 2.5em;
    border-radius: 14px;
  }
</style>
